package com.leon.rose.common.utils;

import java.io.Serializable;
import lombok.Data;

/**
 * 返回数据
 *
 * @author Leon
 */
@Data
public class R<T> implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final int NO_LOGIN = -1;

  public static final int SUCCESS = 0;

  public static final int FAIL = 1;

  public static final int NO_PERMISSION = 2;

  private String msg = "success";

  private int code = SUCCESS;

  private T data;

  public R() {
    super();
  }

  public R(T data) {
    super();
    this.data = data;
  }

  public R(T data, String msg) {
    super();
    this.data = data;
    this.msg = msg;
  }

  public R(Throwable e) {
    super();
    this.msg = e.getMessage();
    this.code = FAIL;
  }

  public static R ok() {
    return new R();
  }

  public static R error() {
    return error(FAIL, "未知异常，请联系管理员");
  }

  public static R error(String msg) {
    return error(FAIL, msg);
  }

  public static R error(int code, String msg) {
    R r = new R();
    r.setCode(code);
    r.setMsg(msg);
    return r;
  }
}
